package com.qnw.CardGroupManagement;

import java.io.*;
import java.util.*;
import com.ygo.feihua.*;

public class File_ydk
{
	static	String[] a=new String[]{};
	static	String[] b=new String[]{};;
	static  String[] aa=new String[]{};
	static  String[] bb=new String[]{};;

	public static final String DECK_MAIN="#main";
	public static final String DECK_EX="#extra";
	public static final String DECK_SIDE="!side";
	

	public static String[][] messageYdk(String message){
		String[][] kz=new String[][]{{},{},{}};
		int i1=message.indexOf(DECK_MAIN);
		int i2=message.indexOf(DECK_EX);
		int i3=message.indexOf(DECK_SIDE);
		if(i1!=-1){
			if(i2!=-1){
			kz[0]=message.substring(i1+DECK_MAIN.length(),i2).trim().split("\n");
			}else if(i3!=-1){
			kz[0]=message.substring(i1+DECK_MAIN.length(),i3).trim().split("\n");
			}else{
				kz[0]=message.substring(i1+DECK_MAIN.length(),message.length()).trim().split("\n");
			}
		}
		if(i2!=-1){
			if(i3!=-1){
				kz[1]=message.substring(i2+DECK_EX.length(),i3).trim().split("\n");
			}else{
				kz[1]=message.substring(i2+DECK_EX.length(),message.length()).trim().split("\n");
			}
		}
		if(i3!=-1){
			kz[2]=message.substring(i3+DECK_SIDE.length(),message.length()).trim().split("\n");
		}
		
		return kz;
	}
	
	
	//读取ydk的内容,以二维数组分别返回主卡组,额外,副卡组	
	public static String[][] ydk(String path)
	{
		try
		{
			String[][] kazu=new String[][]{{},{},{}};
			//读取ydk的内容,并把每行的值以空格隔开全部赋给a
			String a="";
			String b="";
			String c="";
			int bj=0;
			BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(path)));
			String line=null;
			for (;(line = reader.readLine()) != null;)
			{
				if(line.startsWith("#ex"))
					bj=1;
				if(line.startsWith("!"))
					bj=2;
				if(!line.matches("\\D+"))
				{
					switch(bj){
						case 0:
							a=a+line+" ";
							break;
						case 1:
							b=b+line+" ";
							break;
						case 2:
							c=c+line+" ";
							break;
					}
				}
			}
			reader.close();
			if (a.equals(""))
				kazu[0] = new String[]{};
			else
				kazu[0] = a.split(" ");	
			if (b.equals(""))
				kazu[1] = new String[]{};
			else
				kazu[1] = b.split(" ");	
			if (c.equals(""))
				kazu[2] = new String[]{};
			else
				kazu[2] = c.split(" ");	
			return kazu;			
		}
		catch (IOException e)
		{
			return new String[][]{{},{},{}};
		}
	} 	

	/*卡组名字*/
	public  static String[] ydkname(String path)
	{
	//	String [] l=bb;
		try
		{
			File f = new File(path);
			if (!f.exists())
			{
				return new String[]{} ;
			}

			if (f.isDirectory())
			{

				File[] list = f.listFiles(getFileExtensionFilter(".ydk"));				
				aa = new String[list.length];				
				for (int i = 0; i < list.length; i++)
				{			
					//把File类型转成String
					String ab=list[i].toString();
					//split方法返回数组,元素是原String见/分开
					String abb[]=ab.split("/");
					String c=abb[abb.length - 1];
					//"."是转义字符,必须得加\\
					//String cc[]=c.split("\\.");
					int d=c.lastIndexOf(".");
					if(d!=-1){
						c=c.substring(0,d);
					}
					aa[i] =c;// cc[0];
				}		
				bb = hb(aa, bb);
				//String [] v=bb;
				//如果是目录继续用这个目录执行ydkpath方法
				list = f.listFiles(getNotDirFileFilter());
				for (int i = 0; i < list.length; i++)
				{	
					ydkname(list[i].toString());
				}		
			}

		}
		catch (Exception e){

		}
		return bb;
	}

	/*卡组路径*/
	public  static String[] ydkpath(String path)
	{
		try
		{
			File f = new File(path);
			if (!f.exists())
			{
				return new String[]{} ;
			}

			if (f.isDirectory())
			{

				File[] list = f.listFiles(getFileExtensionFilter(".ydk"));				
				a = new String[list.length];	
				for (int i = 0; i < list.length; i++)
				{			
					//把File类型转成String
					a[i] = list[i].toString();				
				}		
				b = hb(a, b);

				//如果是目录继续用这个目录执行ydkpath方法
				list = f.listFiles(getNotDirFileFilter());
				for (int i = 0; i < list.length; i++)
				{	
					ydkpath(list[i].toString());
				}		
			}

		}
		catch (Exception e){
			
		}	
		return b;
	}


	public static FilenameFilter getFileExtensionFilter(String extension)
	{// 指定扩展名过滤
		final String _extension = extension;
		return new FilenameFilter() {
			public boolean accept(File file, String name)
			{
				boolean ret = name.endsWith(_extension);
				return ret;
			}
		};
	}

	public static FileFilter getNotDirFileFilter()
	{ // 文件还是目录过滤
		return new FileFilter() {
			public boolean accept(File file)
			{
				return file.isDirectory();//关键判断点
			}
		};
	}


	/*合并数组*/
	public static String[] hb(String[] aaa, String[] bbb)
	{
		String[] c=new String[aaa.length + bbb.length];
		for (int i=0;i < c.length;i++)
		{
			if (i < aaa.length)
				c[i] = aaa[i];
			else
				c[i] = bbb[i - aaa.length];
		}
		return c;
	}
	
	public static void clear()
	{
		a = new String[]{};
		b = new String[]{};
		aa = new String[]{};
		bb = new String[]{};
	}
}

